<template>
  <div class="app-container">
    <div class="vip-info">
      <div class="tab-list flex-a">
        <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
          @click="changeTab(item.value)">
          <span>{{ item.name }}</span>
          <i></i>
        </div>
      </div>
      <div class="box">
        <div class="flex-b">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" class="input">
            <el-form-item>
              <el-select v-model="queryParams.name" placeholder="请选择店铺">
                <el-option v-for="item in storeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryParams.card" placeholder="请选择会员卡">
                <el-option v-for="item in cardList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryParams.num" placeholder="请选择次卡">
                <el-option v-for="item in numList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryParams.name" prefix-icon="el-icon-search" style="width:340px"
                placeholder="请输入会员姓名/手机号码/宠物名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
              <el-button type="primary" class="btn" size="mini" @click="handleAdd"
                v-hasPermi="['pet:vipInfo:add']">新增会员</el-button>
              <!-- <el-button type="warning" class="btn" size="mini" @click="handleExport"
                v-hasPermi="['pet:petInfo:export']">导出</el-button> -->
            </el-form-item>
          </el-form>
        </div>
        <div>
          <el-table v-loading="loading" :data="vipInfoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="会员编号" sortable align="center" prop="no" />
            <el-table-column label="会员姓名" align="center" prop="name" />
            <el-table-column label="宠物" align="center" prop="pet" />
            <el-table-column label="手机号码" align="center" prop="phone" />
            <el-table-column label="加入时间" v-if="tab == 'time'" sortable align="center" prop="createTime" />
            <el-table-column label="会员卡余额" v-if="tab == 'money' || tab == 'time'" sortable align="center" prop="money" />
            <el-table-column label="剩余积分" v-if="tab == 'point'" sortable align="center" prop="integral" />
            <el-table-column label="消费金额" v-if="tab == 'useMoney'" sortable align="center" prop="allMoney" />
            <!-- <el-table-column label="消费次数" v-if="tab == 'useNum'" sortable align="center" prop="phone" /> -->
            <el-table-column label="消费时间" v-if="tab == 'useTime'" sortable align="center" prop="updateTime" />
            <el-table-column label="所属店铺" align="center" prop="storeName" />
            <!-- <el-table-column label="服务号推送" align="center" prop="state">
              <template v-slot:header='scope'>
                <span>
                  服务号推送
                  <el-tooltip :aa="scope" class="item" effect="dark" placement="top">
                    <i class="el-icon-question"> </i>
                    <div slot="content">
                      【未绑定】指会员未绑定过公众号； <br />
                      【已绑定】指会员已经成功绑定了公众号。<br />
                    </div>
                  </el-tooltip>
                </span>
              </template>
            </el-table-column> -->
            <el-table-column label="会员状态" align="center" prop="state">
              <template v-slot:header='scope'>
                <span>
                  会员状态
                  <el-tooltip :aa="scope" class="item" effect="dark" placement="top">
                    <i class="el-icon-question"> </i>
                    <div slot="content">
                      普通会员、储值会员 <br />
                    </div>
                  </el-tooltip>
                </span>
              </template>
              <!-- <template slot-scope="scope">
                <el-button v-if="scope.row.state=='未激活'" type="primary" class="btn" style="padding: 0; height: 30px;line-height: 30px;width: 50px;text-align: center;font-size: 12px;" size="mini">激活</el-button>
              </template> -->
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-wallet" v-if="scope.row.state == '激活'"
                  @click="toVipCard(scope.row.id,scope.row.no,scope.row.cardId, 'recharge')">充值</el-button>
                <el-button size="mini" type="text" icon="el-icon-s-finance" v-if="scope.row.state == '未激活'"
                  @click="toVipCard(scope.row.id,scope.row.no)">开卡</el-button>
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['pet:vipInfo:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['pet:vipInfo:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="flex-b">
            <div>
              <el-button type="primary" class="btn" style="position:relative;top:15px"
                @click="handleDelete">批量删除</el-button>
            </div>
            <div>
              <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body class="center">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="input">
        <el-form-item label="会员姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入会员姓名" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="性别" prop="phone">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="会员生日" prop="date">
          <el-date-picker clearable v-model="form.date" style="width:100%" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择会员生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入联系地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="5" maxlength="200" show-word-limit placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="btn" style="padding:12px 70px" @click="submitForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVipInfo, getVipInfo, delVipInfo, addVipInfo, updateVipInfo } from "@/api/pet/vipInfo";

export default {
  name: "VipInfo",
  data() {
    return {
      tab: 'time',
      tabList: [
        {
          name: '按加入时间',
          value: 'time'
        },
        {
          name: '按剩余金额',
          value: 'money'
        },
        {
          name: '按剩余积分',
          value: 'point'
        },
        {
          name: '按消费金额',
          value: 'useMoney'
        },
        // {
        //   name: '按消费次数',
        //   value: 'useNum'
        // },
        {
          name: '按消费时间',
          value: 'useTime'
        },
      ],
      storeList: [],//店铺下拉
      cardList: [],//会员卡下拉
      numList: [],//次卡下拉
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息表格数据
      vipInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        name: null,
        country: null,
        phone: null,
        sex: null,
        date: null,
        address: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员信息列表 */
    getList() {
      this.loading = true;
      listVipInfo(this.queryParams).then(response => {
        this.vipInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    toVipCard(id,no,cardId, type) {
      this.$router.push({ path: '/vip/addCard', query: { id,no,type,cardId } })
    },
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        name: null,
        country: null,
        phone: null,
        sex: null,
        date: null,
        address: null,
        remark: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVipInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVipInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVipInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员信息编号为"' + ids + '"的数据项？').then(function () {
        return delVipInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/vipInfo/export', {
        ...this.queryParams
      }, `vipInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
